package com.rfid.project.web;

import java.text.DateFormat;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.exception.CannotDelException;
import com.rfid.project.model.Product;
import com.rfid.project.service.ProductService;
import com.rfid.project.model.ProductCategory;
import com.rfid.project.service.ProductCategoryService;

@Controller
public class ProductController {

	private static final Logger logger = LoggerFactory
			.getLogger(ProductController.class);

	@Autowired
	private ProductService productService;

	@Autowired
	private ProductCategoryService productCategoryService;

	@RequestMapping(value = "/productAnalyse")
	public String productAnalyse(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return "productAnalyse";
	}
	
	@RequestMapping(value = "/productmgr")
	public String productmgr(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<ProductCategory> productCategorys = productCategoryService.findAll();
		model.addAttribute("productCategorys", productCategorys);
		return "productmgr";
	}
	
	@RequestMapping(value = "/productQuery")
	public String productQuery(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<ProductCategory> productCategorys = productCategoryService.findAll();
		model.addAttribute("productCategorys", productCategorys);
		return "productQuery";
	}
	
	@RequestMapping(value = "/queryProduct")
	public String queryProduct(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam(value = "productCode", defaultValue = "") String productCode,
			@RequestParam(value = "name", defaultValue = "") String name,
			@RequestParam(value = "spec", defaultValue = "") String spec,
			@RequestParam(value = "productCategoryId", defaultValue = "-1") Long productCategoryId,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "sort", defaultValue = "") String sort,
			@RequestParam(value = "order", defaultValue = "") String order,
			Model model) {
		Page<Product> products = productService.findAll(productCode, name,
				spec, productCategoryId, remark, pageNumber, pageSize, sort, order);
		JSONObject object = new JSONObject();
		object.put("total", products.getTotalElements());
		JSONArray models = new JSONArray();
		for (Product product : products) {
			JSONObject mod = new JSONObject();
			capacityData(product, mod);
			
			models.add(mod);
		}
		object.put("rows", models);
		PageJsonBean.printObject(response, object);
		return null;
	}
	
	@RequestMapping(value = "/findProduct")
	public String findProduct(
			HttpServletRequest request,
			HttpServletResponse response,
			@Valid Product product,
			Model model) {
		Product item = this.productService.findOne(product.getId());
		JSONObject result = new JSONObject();
		result.put("success", true);
		JSONObject mod = new JSONObject();
		capacityData(item, mod);
		result.put("success", true);
		result.put("data", mod);
		PageJsonBean.printObject(response, result);
		return null;
	}

	private void capacityData(Product product, JSONObject mod) {
		if(null == product){
			return;
		}
		mod.put("id", product.getId());
		mod.put("name", product.getName());
		mod.put("productCode", product.getProductCode());
		mod.put("spec", product.getSpec());
		String content = product.getRemark();
		mod.put("remark", content == null?"":content);
		
		mod.put("productCategoryID", product.getProductCategory().getId());
		mod.put("productCategoryName", product.getProductCategory().getName());
	}

	@RequestMapping(value = "/editProduct")
	public String editProduct(HttpServletRequest request,
			HttpServletResponse response, @Valid Product product,
			Model model, RedirectAttributes redirectAttributes) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.productService.saveProduct(product);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delProduct")
	public String delProduct(
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		try {
			this.productService.deleteProduct(id);
		} catch (CannotDelException e) {
			flag = false;
			result.put("msg", e.getMessage());
		}
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat();
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

}
